require 'View/UIBase/UIBase'

UIRuneBaptism = UIBase.New(PanelNames.UIRuneBaptism)
local specialIndex = 6
function UIRuneBaptism.Init(partnerId, slotIndex)
    UIRuneBaptism.partnerId = partnerId
    UIRuneBaptism.index = slotIndex or 0
    UIBase.Init(UIRuneBaptism)
end

function UIRuneBaptism:OnCreate()
    self.txtName = self:GetChild("name")
    self.btnRuneItem = self:GetChild("Debris")
    self.btnHelp = self:GetChild("help")
    self.btnProbability = self:GetChild("btnProbability")
    self.btnBaptism = self:GetChild("btnQuickChoose1")
    self.btnBack = self:GetChild("btnBack")
    self.listAttr = self:GetChild("list")
    self.listCostItems = self:GetChild("itemList")
    self.btnLeftArrow = self:GetChild("n158")
    self.btnRightArrow = self:GetChild("n157")
    self.effBaptism = self:GetChild("effect_01")
    self:AddEvent()
    self:InitView()
    self:Refresh()
end

function UIRuneBaptism:AddEvent()
    self:RegisterOnClick(self.btnBack,self.OnClickClose)
    self:RegisterOnClick(self.btnHelp,self.OnClickHelp)
    self:RegisterOnClick(self.btnProbability,self.OnClickProbability)
    self:RegisterOnClick(self.btnBaptism,self.OnClickBaptism)
    self:RegisterOnClick(self.btnLeftArrow,self.OnClickLeftArrow)
    self:RegisterOnClick(self.btnRightArrow,self.OnClickRightArrow)
    self:SetDynamicList(self.listAttr ,self.AttrRender)
    self:SetDynamicList(self.listCostItems ,self.ItemRender)
    self:RegisterEvent(Config.EventType.FreshHeroRuneBaptism,self.BaptismRefresh)
    self:RegisterEvent(Config.EventType.FreshHeroRuneLock,self.Refresh)
end

function UIRuneBaptism:InitView()
end

function UIRuneBaptism:BaptismRefresh()
    self:Refresh()
    local aniWrap = self.effBaptism.wrapTarget
    Util.PlayAction(aniWrap, "idle", false)
end

function UIRuneBaptism:Refresh()
    self.runeInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId, self.index)
    if not self.runeInfo then return end
    self.runeOriginCfg = logicMgr.RuneManager.GetRuneCfgInfo(tostring(self.runeInfo.itemModelId))
    self.txtName.text = self.runeInfo.name
    logicMgr.RuneManager.SetRuneItemData(self.btnRuneItem, self.runeInfo)
    self.attrList = self.runeInfo.baptismAttr
    self.lockList = self.runeInfo.lockList or {}
    if #self.attrList == 0 then
        self.listAttr.numItems = tonumber(self.runeOriginCfg.f_RuneRefineNum)
    else
        self.listAttr.numItems = #self.attrList
    end
    local lockNum = self:GetLockNum(self.lockList)
    self.costItemList = self.runeOriginCfg.f_RuneRefineCost[lockNum + 1] or {}
    if self.costItemList then
        self.listCostItems.numItems = #self.costItemList
    end
end

function UIRuneBaptism:AttrRender(index, go)
    local txtName = go:GetChild("title")
    local ctrQuality = go:GetController("Quality")
    local btnLock = go:GetChild("btn_lock")
    local barProportion = go:GetChild("n103")
    local txtProportion = go:GetChild("n94")
    local ctrlType = go:GetController("type")
    local baptismInterval =  self.runeOriginCfg.f_RuneRefine
    ctrlType.selectedIndex = 0
    if #self.attrList == 0 then
        ctrQuality.selectedIndex = 2
    else
        ctrQuality.selectedIndex = 0
        local data = self.attrList[index]
        if data and baptismInterval then
            local at = G.dataTable["t_attributelibrary"][tostring(data[1])]
            if at then
                local type = at.f_AttributeType
                if type == 2 then
                    txtName.text =  at.f_AttributeName.." + "..data[2]*0.01 .. "%"  --"+" .. delta * 0.01 .. "%"
                else
                    txtName.text = at.f_AttributeName.." + "..data[2]
                end
            end
            local min = 0
            local max = 0
            for k,v in pairs(baptismInterval) do
                local find = true
                local cfgAttrList = v
                for m,n in pairs(cfgAttrList) do
                    if n[1] == data[1] then
                        find = true
                        min = n[2] or 0
                        max = n[3] or 0
                        break
                    end
                end
                if find then
                    local type = at.f_AttributeType
                    if type == 2 then
                        txtProportion.text =  min*0.01 .. "%" .. "~" .. max*0.01 .. "%"  --"+" .. delta * 0.01 .. "%"
                    else
                        txtProportion.text = min.."~"..max
                    end
                    barProportion.max = max
                    barProportion.min = min
                    barProportion.value = data[2]--(data[2]-min)/(max-min)
                    break
                end
            end
            btnLock.onClick:Clear()
            btnLock.onClick:Add(function()
                runeHandler.reqRuneLock(self.runeInfo.partnerId, self.runeInfo.itemId, self.runeInfo.index, index-1)
            end)
        end
        if self.lockList[index] and self.lockList[index] == 1 then
            btnLock.selected = true
        else
            btnLock.selected = false
        end
    end
end

function UIRuneBaptism:ItemRender(index, go)
    local item = go:GetChild("n0")
    local txtHave = go:GetChild("text_price")
    local txtNeed = go:GetChild("text_own")
    local ctrlEnough = go:GetController("isEnough")
    local data = self.costItemList[index]
    if data then
        local haveNum = logicMgr.ItemManager.getBagNumByTableID(data[1])
        local needNum = tonumber(data[2])
        txtHave.text =  haveNum
        txtNeed.text = needNum
        if haveNum >= needNum then
            ctrlEnough.selectedIndex = 0
        else
            ctrlEnough.selectedIndex = 1
        end
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.Item);
        if ctrlItem ~= nil and data[1] then
            ctrlItem.initData(item,logicMgr.CostManager.getItemOrEquipDataByIdAndNum(data[1], data[2]))
            ctrlItem.setNum(item,"")
        end
    end
end

function UIRuneBaptism:OnClickBaptism()
    local bMoneyEnough = true
    for k,v in pairs(self.costItemList) do
        local haveNum = logicMgr.ItemManager.getBagNumByTableID(v[1])
        local need = v[2]
        if need > haveNum then
            bMoneyEnough = false
            break
        end
    end
    local attrCount = #self.lockList
    local lockNum = self:GetLockNum(self.lockList)
    if attrCount == lockNum and lockNum > 0 then
        UISysTips.AddMsg("所有属性都已锁定，无法洗练")
        return
    end
    if not bMoneyEnough then
        UISysTips.AddMsg("资源不足")
        return
    end
    runeHandler.reqBaptismRune(self.runeInfo.partnerId, self.runeInfo.itemId, self.runeInfo.index,0)
end


function UIRuneBaptism:GetLockNum(tab)
    if tab then
        local num = 0
        for k,v in pairs(tab) do
            if v == 1 then
                num = num + 1
            end
        end
        return num
    else
        return 0
    end
end

function UIRuneBaptism:OnClickRightArrow()
    --local curIndex = self.index
    --if curIndex and curIndex >= 0 and curIndex <= specialIndex then
    --    if self.index == specialIndex then
    --        self.index = 0
    --    else
    --        self.index = self.index + 1
    --    end
    --    self:Refresh()
    --end
    local showIndex = self:GetShowIndex(1)
    if not (showIndex == self.index) then
        self.index = showIndex
        self:Refresh()
    end
end

function UIRuneBaptism:OnClickLeftArrow()
    --local curIndex = self.index
    --if curIndex and curIndex >= 0 and curIndex <= specialIndex then
    --    if self.index == 0 then
    --        self.index = specialIndex
    --    else
    --        self.index = self.index - 1
    --    end
    --    self:Refresh()
    --end
    local showIndex = self:GetShowIndex(-1)
    if not (showIndex == self.index) then
        self.index = showIndex
        self:Refresh()
    end
end

function UIRuneBaptism:GetShowIndex(LOrR)
    --self.heroRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId)
    --local equipRuneCount = 0
    --for k,v in pairs(self.heroRuneInfo) do
    --    equipRuneCount = equipRuneCount + 1
    --end
    local showIndex = self.index
    if LOrR == 1 then
        for i = 1, 7 do
            showIndex = showIndex + 1
            if showIndex == specialIndex + 1 then
                showIndex = 0
            end
            local runeInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId,showIndex)
            if runeInfo then
                return runeInfo.index
            end
        end
    elseif LOrR == -1 then
        for i = 1, 7 do
            showIndex = showIndex - 1
            if showIndex == -1 then
                showIndex = specialIndex
            end
            local runeInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId,showIndex)
            if runeInfo then
                return runeInfo.index
            end
        end
    end
end

function UIRuneBaptism:OnClickHelp()
    local txts = {}
    txts["title"] = "RuneBaptismTitle"
    txts["des"] = "RuneBaptism"
    showUI(PanelNames.CommonTips, txts)
end

function UIRuneBaptism:OnClickProbability()
    local txts = {}
    txts["title"] = "RuenBaptismProbabilityTitle"
    txts["des"] = "RuenBaptismProbability"
    showUI(PanelNames.CommonTips, txts)
end

function UIRuneBaptism:OnClickClose()
    closeUI(PanelNames.UIRuneBaptism)
end

return UIRuneBaptism